// pages/checkinhistory_student/checkinhistory_student.js
import { addCheckinHistory } from '../../api/api.js';

Page({
    data: {
        classId: '',
        className: '',
        showCheckinModal: false,
        checkinName: '',
        duration: '5', // 默认5分钟
        showClassCodeModal: false,
        classCode: '' // 班级码
    },
    onLoad(options) {
        const { classId, className, classCode } = options;
        this.setData({
            classId,
            className: className || '班级',
            classCode: classCode || ''
        });
        console.log('接收到的 classId:', this.data.classId);
    },
    onShow() {
        // 设置下次显示时确保输入框样式正确
        if (this.data.showCheckinModal) {
            // 如果模态框显示，刷新一下数据确保样式正确
            this.setData({
                checkinName: this.data.checkinName,
                duration: this.data.duration
            });
        }
    },
    navigateToCheckinHistory() {
        const classId = this.data.classId;
        wx.navigateTo({
            url: `/pages/checkinhistory/checkinhistory?classId=${classId}`
        });
    },
    navigateToStudentPage() {
        const classId = this.data.classId;
        wx.navigateTo({
            url: `/pages/studentlist/studentlist?classId=${classId}`
        });
    },

    // 显示班级码模态框
    showClassCodeModal() {
        this.setData({
            showClassCodeModal: true
        });
    },

    // 关闭班级码模态框
    closeClassCodeModal() {
        this.setData({
            showClassCodeModal: false
        });
    },

    // 复制班级码到剪贴板
    copyClassCode() {
        const { classCode } = this.data;
        if (!classCode) {
            wx.showToast({
                title: '班级码为空',
                icon: 'error'
            });
            return;
        }

        wx.setClipboardData({
            data: classCode,
            success: () => {
                wx.showToast({
                    title: '复制成功',
                    icon: 'success'
                });
                this.closeClassCodeModal();
            }
        });
    },

    // 显示添加签到模态窗口
    showAddCheckinModal() {
        this.setData({
            showCheckinModal: true,
            checkinName: '',
            duration: '5' // 重置为默认值
        });
    },

    // 监听签到名称输入
    onCheckinNameInput(e) {
        this.setData({
            checkinName: e.detail.value
        });
    },

    // 监听签到持续时间输入
    onDurationInput(e) {
        this.setData({
            duration: e.detail.value
        });
    },

    // 取消添加签到
    cancelAddCheckin() {
        this.setData({
            showCheckinModal: false
        });
    },

    // 确认添加签到
    confirmAddCheckin() {
        const { checkinName, duration, classId } = this.data;

        // 验证持续时间
        if (!duration || isNaN(Number(duration)) || Number(duration) <= 0) {
            wx.showToast({
                title: '请输入有效的时间',
                icon: 'error'
            });
            return;
        }

        wx.showLoading({
            title: '创建中...'
        });

        // 调用API创建签到历史记录，增加了签到名称和持续时间参数
        addCheckinHistory(classId, checkinName, Number(duration)).then(res => {
            wx.hideLoading();
            wx.showToast({
                title: '创建成功',
                icon: 'success'
            });

            this.setData({
                showCheckinModal: false
            });

            // 跳转到签到历史页面
            setTimeout(() => {
                this.navigateToCheckinHistory();
            }, 1000);
        }).catch(err => {
            wx.hideLoading();
            console.error('创建签到失败:', err);
            wx.showToast({
                title: '创建失败',
                icon: 'error',
                duration: 2000
            });
        });
    },

    navigateBack() {
        wx.navigateBack();
    }
});